package com.myway.core.jersey.client;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.util.zip.GZIPInputStream;

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.SecurityContext;

import org.glassfish.jersey.internal.util.Base64;

/**
 * @ClassName: GzipClient <br/>
 * @Description: Request gzip returned by the server <br/>
 * @CreateDate: 2016年4月27日 下午5:24:39 <br/>
 * @author Toby
 * @version V1.0
 */
public class GzipClient {
	
		//提交地址
		private static final String PATH = "http://192.168.5.104:8081/CommonServices/resource/restWebservice/common";
		
		//提交参数
		private static String json = "id=#{id}&param=#{param}";
		
		
		/**
		 * @param path 路径
		 * @param json json参数
		 * @param isproxy 是否请求代理
		 * @param isasin 是否需要签名
		 * @return
		 */
		public static String post(String path,String json, boolean isproxy,boolean isasin) {
//			String add_url = "http://127.0.0.1:8080/CommonService/resouce/simpleresouce/"+ path;
//			String json = "{'id':'adfasdf','name':'wwww','message':'猜猜我是谁'}";
			try {
				URL url = new URL(path);
				HttpURLConnection connection=null;
				//是否请求代理
				if(isproxy){
					InetSocketAddress addr = new InetSocketAddress("127.0.0.1",8888);
					Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); //
					connection = (HttpURLConnection)url.openConnection(proxy);
				}else{
					connection = (HttpURLConnection) url.openConnection();
				}
				connection.setDoInput(true);
				connection.setDoOutput(true);
				connection.setRequestMethod("POST");
				connection.setUseCaches(false);
				//base64 加密用户名 密码
				String author="admin:q1w2e3";
				connection.setRequestProperty(HttpHeaders.AUTHORIZATION, "Basic "+new String(Base64.encode(author.getBytes()), "UTF-8")); 
				connection.setRequestProperty(SecurityContext.FORM_AUTH, "B2C");
				//设置http请求的数据类型
				connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, "text/html");
				
				//是否需要签名
				if (isasin) {
					connection.setRequestProperty("client-name", "CommonService");
				}
				connection.connect();
				if(connection.getRequestMethod().equals("POST")){
					OutputStream out = connection.getOutputStream();
					Writer wout = new OutputStreamWriter(out, "utf-8");
					wout.write(json);
					wout.flush();
					wout.close();
				}
				ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
		        GZIPInputStream gunzip = new GZIPInputStream(connection.getInputStream()); 
		        byte[] buffer = new byte[1024];
	            int n; 
	            while ((n = gunzip.read(buffer)) >= 0) {
	            	bos.write(buffer, 0, n); 
	            } 
		       /*return  bos.toString();*/
		       
		       	byte[] data = bos.toByteArray();
		       	String result = new String(data,"utf-8"); 
		     	return result;
			} catch (Exception e) {
				return e.getMessage();
			}
		}
		public static String post(String path,String json, boolean isproxy,boolean isasin,String clientName) {
//			String add_url = "http://127.0.0.1:8080/CommonService/resouce/simpleresouce/"+ path;
//			String json = "{'id':'adfasdf','name':'wwww','message':'猜猜我是谁'}";
			try {
				URL url = new URL(path);
				HttpURLConnection connection=null;
				//是否请求代理
				if(isproxy){
					InetSocketAddress addr = new InetSocketAddress("127.0.0.1",8888);
					Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); //
					connection = (HttpURLConnection)url.openConnection(proxy);
				}else{
					connection = (HttpURLConnection) url.openConnection();
				}
				connection.setDoInput(true);
				connection.setDoOutput(true);
				connection.setRequestMethod("POST");
				connection.setUseCaches(false);
				//base64 加密用户名 密码
				String author="admin:q1w2e3";
				connection.setRequestProperty(HttpHeaders.AUTHORIZATION, "Basic "+new String(Base64.encode(author.getBytes()), "UTF-8")); 
				connection.setRequestProperty(SecurityContext.FORM_AUTH, clientName);
				//设置http请求的数据类型
				connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, "text/html");
				
				//是否需要签名
				if (isasin) {
					connection.setRequestProperty("client-name", "CommonService");
				}
				connection.connect();
				if(connection.getRequestMethod().equals("POST")){
					OutputStream out = connection.getOutputStream();
					Writer wout = new OutputStreamWriter(out, "utf-8");
					wout.write(json);
					wout.flush();
					wout.close();
				}
				ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
				GZIPInputStream gunzip = new GZIPInputStream(connection.getInputStream()); 
				byte[] buffer = new byte[1024];
				int n; 
				while ((n = gunzip.read(buffer)) >= 0) {
					bos.write(buffer, 0, n); 
				} 
				/*return  bos.toString();*/
				
				byte[] data = bos.toByteArray();
				String result = new String(data,"utf-8"); 
				return result;
			} catch (Exception e) {
				return e.getMessage();
			}
		}

}
